﻿using System;
using System.Linq;
using System.Collections.Generic;

using SimpleForum.Data.Models;
using SimpleForum.Data.Repositories;

namespace SimpleForum.Data.Services
{
    public class PostService : IPostService
    {
        private readonly IRepository<Post> PostRepository;
        private readonly UserService UserService;

        public PostService(IRepository<Post> PostRepository, UserService UserService)
        {
            this.PostRepository = PostRepository;
            this.UserService = UserService;
        }

        public Post GetPost(int Id)
        {
            return PostRepository.Get(Id);
        }

        public Post CreatePost(int ThreadId, int UserId, string Message)
        {
            var User = UserService.GetUser(UserId);
            var Post = new Post
            {
                CreatedDate = DateTime.UtcNow,
                ThreadId = ThreadId,
                CreatorId = UserId,
                Body = Message
            };
            
            // fix this
            //Akismet Akismet = new Utils.Akismet("124da029f693", "www.google.com", "SimpleForum");
            //Post.IsSpam = Akismet.CommentCheck(new Utils.AkismetComment
            //{
            //    ForumURL = "http://www.simpleforum.com",
            //    Referrer = System.Web.HttpContext.Current.Request.UrlReferrer.ToString(),
            //    UserAgent = System.Web.HttpContext.Current.Request.UserAgent,
            //    UserIP = System.Web.HttpContext.Current.Request.UserHostAddress,
            //    Post = Post,
            //    User = User
            //});

            UserService.AddReputation(User, UserService.ReputationType.ReplyThread, "thread/" + ThreadId + "#post" + Post.Id);

            return PostRepository.Add(Post);
        }

        public bool RatePost(int PostId, int UserId, string Rating)
        {
            var Post = GetPost(PostId);

            if (Post != null)
            {
                Post.Ratings.Add(new PostRating
                {
                    PostId = PostId,
                    UserId = UserId,
                    RatingName = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Rating)
                });

                PostRepository.Update(Post);

                return true;
            }
            else return false;
        }

        public bool ReportPost(int PostId, int UserId, string ReportReason, string Reason)
        {
            var Post = GetPost(PostId);

            if (Post != null)
            {
                Post.Reports.Add(new PostReport
                {
                    PostId = PostId,
                    UserId = UserId,
                    ReportReason = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(ReportReason),
                    Reason = Reason,
                    DateReported = DateTime.Now
                });

                PostRepository.Update(Post);
                UserService.AddReputation(UserId, UserService.ReputationType.ReportPost, "thread/" + Post.ThreadId + "#post" + Post.Id);

                return true;
            }
            else return false;
        }
    }
}